package com.example.day7;

import androidx.annotation.NonNull;
import androidx.annotation.Nullable;
import androidx.annotation.RequiresApi;
import androidx.appcompat.app.ActionBarDrawerToggle;
import androidx.appcompat.widget.Toolbar;
import androidx.drawerlayout.widget.DrawerLayout;
import androidx.recyclerview.widget.DividerItemDecoration;
import androidx.recyclerview.widget.GridLayoutManager;
import androidx.recyclerview.widget.LinearLayoutManager;
import androidx.recyclerview.widget.RecyclerView;
import androidx.recyclerview.widget.StaggeredGridLayoutManager;

import android.annotation.SuppressLint;
import android.content.Intent;
import android.os.Build;
import android.view.ContextMenu;
import android.view.LayoutInflater;
import android.view.Menu;
import android.view.MenuItem;
import android.view.View;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.Toast;

import com.bumptech.glide.Glide;
import com.example.day7.adapter.FuliAdapter;
import com.example.day7.base.BaseActivity;
import com.example.day7.bean.FuliBean;
import com.example.day7.presenter.FuliPresenter;
import com.example.day7.view.FuliView;
import com.google.android.material.navigation.NavigationView;

import java.util.ArrayList;

import butterknife.BindView;

public class MainActivity extends BaseActivity<FuliPresenter> implements FuliView {
    @BindView(R.id.line)
    LinearLayout Mline;
    @BindView(R.id.toolbar)
    Toolbar Mtoolbar;
    @BindView(R.id.recycler)
    RecyclerView mrecycler;
    @BindView(R.id.navig)
    NavigationView mnavig;
    @BindView(R.id.dra)
    DrawerLayout mdra;
    private ArrayList<FuliBean.ResultsBean> resultsBeans;
    private FuliAdapter fuliAdapter;

    @Override
    protected void initListener() {

    }

    @Override
    protected void initData() {
        View headerView = mnavig.getHeaderView(0);
        ImageView viewById = headerView.findViewById(R.id.header_image);
        Glide.with(this).load(R.drawable.a).into(viewById);
        viewById.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {

            }
        });
        mnavig.setNavigationItemSelectedListener(new NavigationView.OnNavigationItemSelectedListener() {
            @Override
            public boolean onNavigationItemSelected(@NonNull MenuItem menuItem) {
                switch (menuItem.getItemId()){
                    case R.id.day:
                        startActivity(new Intent(MainActivity.this,Main2Activity.class));
                        break;
                }
                return false;
            }
        });
    }

    @Override
    protected void initView() {
        toolbar();
        mrecycler.setLayoutManager(new LinearLayoutManager(this));
        mrecycler.addItemDecoration(new DividerItemDecoration(this,LinearLayout.VERTICAL));
        resultsBeans = new ArrayList<>();
        fuliAdapter = new FuliAdapter(this, resultsBeans);
        mrecycler.setAdapter(fuliAdapter);
    }

    private void toolbar() {
        Mtoolbar.setTitle("ToolBar");
        setSupportActionBar(Mtoolbar);
        ActionBarDrawerToggle actionBarDrawerToggle = new ActionBarDrawerToggle(this, mdra, Mtoolbar, R.string.app_name, R.string.app_name);
        mdra.addDrawerListener(actionBarDrawerToggle);
        actionBarDrawerToggle.syncState();
        mdra.addDrawerListener(new DrawerLayout.DrawerListener() {
            @Override
            public void onDrawerSlide(@NonNull View drawerView, float slideOffset) {
                Mline.setX(mdra.getWidth()*slideOffset);
            }

            @Override
            public void onDrawerOpened(@NonNull View drawerView) {

            }

            @Override
            public void onDrawerClosed(@NonNull View drawerView) {

            }

            @Override
            public void onDrawerStateChanged(int newState) {

            }
        });
    }

    @Override
    public boolean onCreateOptionsMenu(Menu menu) {
        getMenuInflater().inflate(R.menu.menu1,menu);
        return super.onCreateOptionsMenu(menu);
    }

    @Override
    public boolean onOptionsItemSelected(@NonNull MenuItem item) {
        switch (item.getItemId()){
            case R.id.lin:
                mrecycler.setLayoutManager(new LinearLayoutManager(this));
                fuliAdapter.notifyDataSetChanged();
                break;
            case R.id.g:
                mrecycler.setLayoutManager(new GridLayoutManager(this,2));
                fuliAdapter.notifyDataSetChanged();
                break;
            case R.id.s:
                mrecycler.setLayoutManager(new StaggeredGridLayoutManager(2,StaggeredGridLayoutManager.VERTICAL));
                break;
        }
        return super.onOptionsItemSelected(item);
    }

    @Override
    protected void initPreseneter() {
        mPresenter=new FuliPresenter();
        mPresenter.getdata();
    }

    @Override
    protected int getLayout() {
        return R.layout.activity_main;
    }

    @Override
    public void onSuccess(FuliBean data) {
        resultsBeans.addAll(data.getResults());
        fuliAdapter.notifyDataSetChanged();
    }

    @Override
    public void onFali(String error) {
        Toast.makeText(this,error,Toast.LENGTH_LONG).show();
    }

    @Override
    public void showToast(String str) {
        Toast.makeText(this,str,Toast.LENGTH_LONG).show();

    }
}
